package com.summer.service.impl;

import com.summer.entity.OrderCollection;
import com.summer.entity.OrderCollectionReduction;
import com.summer.mapper.OrderCollectionDAO;
import com.summer.mapper.OrderCollectionReductionDAO;
import com.summer.service.IOrderReductionService;
import com.summer.variable.Constant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Desc:
 * Created by tl on 2019/1/5
 */
@Service
public class OrderReductionService implements IOrderReductionService {
    @Resource
    private OrderCollectionReductionDAO orderCollectionReductionDAO;
    @Resource
    private OrderCollectionDAO orderCollectionDAO;

    @Override
    @Transactional
    public void reductConfirm(Map<String, Object> params, OrderCollectionReduction orderCollectionReduction) {
        OrderCollection orderCollection =
                orderCollectionDAO.selectByPrimaryKey(orderCollectionReduction.getCollectionId());
        Byte auditStatus = params.get("auditStatus") == null ? (byte) 0 :
                Byte.parseByte(params.get("auditStatus").toString());
        OrderCollectionReduction newReduction = new OrderCollectionReduction();
        OrderCollection newOrder = new OrderCollection();
        newOrder.setId(orderCollection.getId());
        newReduction.setId(orderCollectionReduction.getId());
        if (Constant.COLLECTION_AUDIT_PASS == auditStatus) {
            newReduction.setAuditStatus(Constant.COLLECTION_AUDIT_PASS);
            newOrder.setReductionMoney(orderCollection.getReductionMoney() + orderCollectionReduction.getReductionAmount());

        } else if (Constant.COLLECTION_AUDIT_REJECT == auditStatus) {
            newReduction.setAuditStatus(Constant.COLLECTION_AUDIT_REJECT);
        }
        Object remarkObj = params.get("remark");
        if (remarkObj != null) {
            newReduction.setRemark(remarkObj.toString());
        }
        orderCollectionReductionDAO.updateByPrimaryKeySelective(newReduction);
        orderCollectionDAO.updateByPrimaryKeySelective(newOrder);
    }
}
